﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Arcadium.Graphics;
using Arcadium.Audio;

namespace Arcadium.GameEntities
{
    class Crate : GameEntity
    {
        #region Field

        private static Texture2D texture;
        private static Vector2 textureOrigin;

        private int previousSpawnAreaIndex = -1;
        private Vector2 position;

        // reward



        #endregion

        #region Properties

        public Vector2 Position { get { return position; } set { position = value; } }

        public float Width { get { return texture.Width; } }

        #endregion

        #region Initialization

        public Crate(World world) : base(world)
        {
            this.position = world.Map.GetRandomSpawnPoint(ref previousSpawnAreaIndex, texture.Width);
        }
        public Crate(World world, Vector2 position) : base(world)
        {
            this.position = position;
        }

        public static void LoadContent(ContentManager content)
        {
            texture = content.Load<Texture2D>("Gameplay\\crateTexture");
            textureOrigin = new Vector2(27, 54);
        }

        #endregion

        public Rectangle GetBoundingRectangle()
        {
            return new Rectangle((int)(position - textureOrigin).X, 
                (int)(position - textureOrigin).Y, 
                texture.Width, texture.Height); 
        }

        public void Renew()
        {
            AudioManager.Crate.Play();
            this.position = World.Map.GetRandomSpawnPoint(ref previousSpawnAreaIndex, Width);

            // TODO:  Only for host. How can we asure it? 
            // local -> host(I picked up this) -> All local(This dude is awesome, here's the new box)
            //World.AddChange(new WorldChange(WorldChange.ChangeType.CrateRelocated,
            //    World.Crates.IndexOf(this), Position));
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(texture, View.Adjust(position), null, Color.White, 0, 
                textureOrigin, View.ScreenScale, SpriteEffects.None, 0);
            base.Draw(spriteBatch);
        }

        

    }
}
